package com.server.controller;

import com.common.context.BaseContext;
import com.common.result.Result;
import com.server.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/common")
@Slf4j
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    CommonService commonService;
    //收藏和点赞

    @GetMapping("/praise")
    @ApiOperation("点赞")
    public Result praise(Integer articleId){
        Integer userId = BaseContext.getCurrentId();
        commonService.addPraise(articleId,userId);
        return Result.success();
    }

    @GetMapping("/praiseCount")
    @ApiOperation("获取点赞数")
    public Result getPraiseCount(Integer articleId){
        Integer praiseCount = commonService.getPraiseCount(articleId);
        return Result.success(praiseCount);
    }

    @GetMapping("/isPraised")
    @ApiOperation("当前用户是否点过赞")
    public Result isPraised(Integer articleId){
        Integer userId = BaseContext.getCurrentId();
        Integer praised = commonService.isPraised(articleId, userId);
        return Result.success(praised);
    }

    @GetMapping("/collect")
    @ApiOperation("收藏文章")
    public Result collectArticle(Integer articleId){
        Integer userId = BaseContext.getCurrentId();
        commonService.addCollect(articleId,userId);
        return Result.success();
    }

    @GetMapping("/collectedCount")
    @ApiOperation("获取收藏数")
    public Result getCollectedCount(Integer articleId){
        Integer collectedCount = commonService.getCollectedCount(articleId);
        return  Result.success(collectedCount);
    }

    @GetMapping("/isCollected")
    @ApiOperation("当前用户是否收藏过文章")
    public Result isCollected(Integer articleId){
        Integer userId = BaseContext.getCurrentId();
        Integer collected = commonService.isCollected(articleId, userId);
        return Result.success(collected);
    }
}

